<template>
  <div
    v-show="showBox"
    :class="mode === 'pop' ? 'verifybox' : ''"
    :style="{ 'max-width': `${parseInt(imgSize.width) + 30}px` }">
    <div v-if="mode === 'pop'" class="verifybox-top">
      请完成安全验证
      <span class="verifybox-close" @click="closeBox">
        <i class="iconfont icon-close"></i>
      </span>
    </div>
    <div class="verifybox-bottom" :style="{ padding: mode === 'pop' ? '15px' : '0' }">
      <!-- 验证码容器 -->
      <Component
        :is="componentType"
        v-if="componentType"
        ref="instance"
        :captcha-type="captchaType"
        :type="verifyType"
        :figure="figure"
        :arith="arith"
        :mode="mode"
        :v-space="vSpace"
        :explain="explain"
        :img-size="imgSize"
        :block-size="blockSize"
        :bar-size="barSize" />
    </div>
  </div>
</template>

<script type="text/babel">
  /**
   * Verify 验证码组件
   * @description 分发验证码使用
   * */
  import VerifySlide from './verify/VerifySlide'
  import VerifyPoints from './verify/VerifyPoints'

  export default {
    name: 'Verify',
    components: {
      VerifySlide,
      VerifyPoints
    },
    props: {
      // 双语化
      locale: {
        require: false,
        type: String,
        default() {
          // 默认语言不输入为浏览器语言
          let language
          if (navigator.language) {
            language = navigator.language
          } else {
            language = navigator.browserLanguage
          }
          return language
        }
      },
      captchaType: {
        type: String,
        required: true
      },
      figure: {
        type: Number
      },
      arith: {
        type: Number
      },
      mode: {
        type: String,
        default: 'pop'
      },
      vSpace: {
        type: Number
      },
      explain: {
        type: String
      },
      imgSize: {
        type: Object,
        default() {
          return {
            width: '310px',
            height: '155px'
          }
        }
      },
      blockSize: {
        type: Object
      },
      barSize: {
        type: Object
      }
    },
    data() {
      return {
        // showBox:true,
        clickShow: false,
        // 内部类型
        verifyType: undefined,
        // 所用组件类型
        componentType: undefined
      }
    },
    computed: {
      instance() {
        return this.$refs.instance || {}
      },
      showBox() {
        if (this.mode === 'pop') {
          return this.clickShow
        } else {
          return true
        }
      }
    },
    watch: {
      captchaType: {
        immediate: true,
        handler(captchaType) {
          switch (captchaType.toString()) {
            case 'blockPuzzle':
              this.verifyType = '2'
              this.componentType = 'VerifySlide'
              break
            case 'clickWord':
              this.verifyType = ''
              this.componentType = 'VerifyPoints'
              break
          }
        }
      }
    },
    methods: {
      /**
       * i18n
       * @description 兼容vue-i18n 调用$t来转换ok
       * @param {String} text-被转换的目标
       * @return {String} i18n的结果
       * */
      i18n(text) {
        if (this.$t) {
          return this.$t(text)
        } else {
          // 兼容不存在的语言
          const i18n = this.$options.i18n.messages[this.locale] || this.$options.i18n.messages['en-US']
          return i18n[text]
        }
      },
      /**
       * refresh
       * @description 刷新
       * */
      refresh() {
        if (this.instance.refresh) {
          this.instance.refresh()
        }
      },
      closeBox() {
        this.clickShow = false
        this.refresh()
      },
      show() {
        if (this.mode === 'pop') {
          this.clickShow = true
        }
      }
    }
  }
</script>

<style>
  .verifybox {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 1;
    box-sizing: border-box;
    background-color: #fff;
    border: 1px solid #e4e7eb;
    border-radius: 2px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    transform: translate(-50%, -50%);
  }

  .verifybox-top {
    box-sizing: border-box;
    height: 50px;
    padding: 0 15px;
    color: #45494c;
    font-size: 16px;
    line-height: 50px;
    text-align: left;
    border-bottom: 1px solid #e4e7eb;
  }

  .verifybox-bottom {
    box-sizing: border-box;
    padding: 15px;
  }

  .verifybox-close {
    position: absolute;
    top: 13px;
    right: 9px;
    width: 24px;
    height: 24px;
    text-align: center;
    cursor: pointer;
  }

  .mask {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1001;
    width: 100%;
    height: 100vh;

    /* display: none; */
    transition: all 0.5s;
  }

  .verify-tips {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 30px;
    color: #fff;
    line-height: 30px;
  }

  .suc-bg {
    background-color: rgba(92, 184, 92, 0.5);
    filter: progid:dximagetransform.microsoft.gradient(startcolorstr=#7f5CB85C, endcolorstr=#7f5CB85C);
  }

  .err-bg {
    background-color: rgba(217, 83, 79, 0.5);
    filter: progid:dximagetransform.microsoft.gradient(startcolorstr=#7fD9534F, endcolorstr=#7fD9534F);
  }

  .tips-enter,
  .tips-leave-to {
    bottom: -30px;
  }

  .tips-enter-active,
  .tips-leave-active {
    transition: bottom 0.5s;
  }

  /* ---------------------------- */

  /* 常规验证码 */
  .verify-code {
    margin-bottom: 5px;
    font-size: 20px;
    text-align: center;
    border: 1px solid #ddd;
    cursor: pointer;
  }

  .cerify-code-panel {
    height: 100%;
    overflow: hidden;
  }

  .verify-code-area {
    float: left;
  }

  .verify-input-area {
    float: left;
    width: 60%;
    padding-right: 10px;
  }

  .verify-change-area {
    float: left;
    line-height: 30px;
  }

  .varify-input-code {
    display: inline-block;
    width: 100%;
    height: 25px;
  }

  .verify-change-code {
    color: #337ab7;
    cursor: pointer;
  }

  .verify-btn {
    width: 200px;
    height: 30px;
    margin-top: 10px;
    color: #fff;
    background-color: #337ab7;
    border: none;
  }

  /* 滑动验证码 */
  .verify-bar-area {
    position: relative;
    box-sizing: content-box;
    text-align: center;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
  }

  .verify-bar-area .verify-move-block {
    position: absolute;
    top: 0;
    left: 0;
    box-sizing: content-box;
    background: #fff;
    border-radius: 1px;
    box-shadow: 0 0 2px #888;
    cursor: pointer;
  }

  .verify-bar-area .verify-move-block:hover {
    color: #fff;
    background-color: #337ab7;
  }

  .verify-bar-area .verify-left-bar {
    position: absolute;
    top: -1px;
    left: -1px;
    box-sizing: content-box;
    background: #f0fff0;
    border: 1px solid #ddd;
    cursor: pointer;
  }

  .verify-img-panel {
    position: relative;
    box-sizing: content-box;
    margin: 0;
    border-top: 1px solid #ddd;
    border-bottom: 1px solid #ddd;
    border-radius: 3px;
  }

  .verify-img-panel .verify-refresh {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 2;
    width: 25px;
    height: 25px;
    padding: 5px;
    text-align: center;
    cursor: pointer;
  }

  .verify-img-panel .icon-refresh {
    color: #fff;
    font-size: 20px;
  }

  .verify-img-panel .verify-gap {
    position: relative;
    z-index: 2;
    background-color: #fff;
    border: 1px solid #fff;
  }

  .verify-bar-area .verify-move-block .verify-sub-block {
    position: absolute;
    z-index: 3;
    text-align: center;
  }

  .verify-bar-area .verify-move-block .verify-icon {
    font-size: 18px;
    pointer-events: none;
  }

  .verify-bar-area .verify-msg {
    z-index: 3;
  }

  .iconfont {
    font-size: 16px;
    font-family: iconfont !important;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  .icon-check::before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 9999;
    display: block;
    width: 16px;
    height: 16px;
    margin: auto;
    background-image: url('');
    background-size: contain;
    content: ' ';
  }

  .icon-close::before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 9999;
    display: block;
    width: 16px;
    height: 16px;
    margin: auto;
    background-image: url('');
    background-size: contain;
    content: ' ';
  }

  .icon-right::before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 9999;
    display: block;
    width: 16px;
    height: 16px;
    margin: auto;
    background-image: url('');
    background-size: contain;
    content: ' ';
  }

  .icon-refresh::before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 9999;
    display: block;
    width: 16px;
    height: 16px;
    margin: auto;
    background-image: url('');
    background-size: contain;
    content: ' ';
  }
</style>
